package de.justphil.tcg.tcgserver.game.mechanics.game;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import de.justphil.tcg.tcgserver.game.mechanics.config.Config;
import org.slf4j.Logger;
import static de.justphil.tcg.tcgserver.commons.logging.TCGLogging.*;

public class BoardPosition implements GameDataSerializable{

	private static final Logger log = gameLogger(BoardPosition.class);
	private int player;
	private int slot;

	public BoardPosition() {}
	
	public BoardPosition(int player, int slot) {

		setPlayer(player);
		setSlot(slot);
	}

	public int getPlayer() {
		return player;
	}

	public void setPlayer(int player) {

		if (player < 0 && player >= Config.NUM_PLAYERS - 1) {

			log.error("Trying to set the player of a CardPosition to {}!",
					player);
		}

		assert !(player < 0 && player >= Config.NUM_PLAYERS - 1) : "Setting the player of a CardPosition to "
				+ player + "!";

		this.player = player;
	}

	public int getSlot() {
		return slot;
	}

	public void setSlot(int slot) {

		if (slot < 0 && slot >= Config.NUM_PLAYERS - 1) {

			log.error("Trying to set the slot of a CardPosition to {}!", slot);
		}

		assert !(slot < 0 && slot >= Config.NUM_SLOTS_PER_PLAYER - 1) : "Setting the slot of a CardPosition to "
				+ slot + "!";

		this.slot = slot;
	}

	/**
	 * Two BoardPositions equal each other when their players and slots are
	 * equal.
	 * 
	 */
	@Override
	public boolean equals(Object obj) {

		if (!BoardPosition.class.isInstance(obj)) {

			return false;
		}
		BoardPosition position = (BoardPosition) obj;

		return position.player == this.player && position.slot == this.slot;
	}

	/**
	 * int result = 17; result = 31 * result + player; result = 31 * result +
	 * slot; return result;
	 */
	@Override
	public int hashCode() {

		int result = 17;
		result = 31 * result + player;
		result = 31 * result + slot;
		return result;
	}

	@Override
	public String toString() {

		StringBuffer buffer = new StringBuffer();

		buffer.append("P:");
		buffer.append(getPlayer());
		buffer.append("|S:");
		buffer.append(getSlot());

		return new String(buffer);
	}

	@Override
	public void writeData(Game game, DataOutput out) throws IOException {
		out.writeInt(player);
		out.writeInt(slot);
	}

	@Override
	public void readData(Game game, DataInput in) throws IOException {
		player = in.readInt();
		slot = in.readInt();
	}
}
